const modalPlugin = requirePlugin('xlightPlugin');
Component({
	props: {
	  
			onRenderFail:()=>{},
			  onRenderSuccess: () => { },
			 
	},
	data: {
		extInfo:{},
		interstitialAd: {},
		oder: "",
		a: "",
		phone: "",
		src: "",
		param: "",
		nomobile: '',
		orderNo:'',
		spaceCode:'',
		type:"",
	},
	onInit() {
my.setStorageSync({
				key: 'spaceCode',
				data: '50_2023111525000070727'
			})
		console.log(my.getStorageSync({
			key: 'orderNo'
		}).data,'获取订单')
		const ad = new modalPlugin.CreateInterstitialAd();
		this.setData({
			interstitialAd: ad,
		})
		if (my.getStorageSync({
				key: 'phone'
			}).data) {
			this.setData({
				phone: my.getStorageSync({
					key: 'phone'
				}).data
			})
		}
		if (my.getStorageSync({
				key: 'a'
			}).data) {
			this.setData({
				a: my.getStorageSync({
					key: 'a'
				}).data
			})
		}
		if (my.getStorageSync({
				key: 'param'
			}).data) {
			this.setData({
				param: my.getStorageSync({
					key: 'param'
				}).data
			})
		}

		
		//如果有订单号就计数，没有就生成
	
	if(my.getStorageSync({
			key: 'orderNo'
		}).data){
	
		this.setData({
			orderNo:my.getStorageSync({
			key: 'orderNo'
		}).data
		})
	my.request({
	  url: 'https://www.sx2b2c.com/api/Alipayad/addNum', // 请求的 URL
	  method: 'POST', // POST 方法
	  data: {
		orderNo:this.data.orderNo
	  },
	  header: {
	    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
	  },
	  success:(result)=> {
	
	  },
	  fail: function() {
	
	  }
	})
	}else{
	
		var a = new Date().getFullYear() + (new Date().getMonth() + 1).toString().padStart(2, "0") +
				new Date().getDate().toString().padStart(2, "0")
			//  "2020-02-12"   获取的是当前日期
			var b = new Date().getHours().toString().padStart(2, "0") + new Date().getMinutes().toString()
				.padStart(2, "0") + new Date().getSeconds().toString().padStart(2, "0")
			//11:13:02当前时间
		
			var randomNum = "" + Math.round(Math.random() * 900000000);
			while (randomNum.length < 8) {
				randomNum = "0" + randomNum;
			}
			const number = 'AD' + a + b + randomNum
		this.setData({
				orderNo:number
		})
			my.setStorageSync({
				key: 'orderNo',
				data: this.data.orderNo
			});

		my.request({
		  url: 'https://www.sx2b2c.com/api/Alipayad/subOrder', // 请求的 URL
		  method: 'POST', // POST 方法
		  data: {
		    a:this.data.a, 
		    param:this.data.param,
			mobile:this.data.phone,
			nomobile:'1',
			from_t:'3',
			from_env:"0",
			orderNo:this.data.orderNo
		  },
		  header: {
		    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
		  },
		  success:(res)=> {
			 my.request({
			   url: 'https://www.sx2b2c.com/api/Alipayad/addNum', // 请求的 URL
			   method: 'POST', // POST 方法
			   data: {
			 	orderNo:this.data.orderNo
			   },
			   header: {
			     'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			   },
			   success:(result)=> {
			
			   },
			   fail: function() {
			 
			   }
			 })

		  
			
		  },
		  fail: function(res) {
		    
		  }
		})
	}
	this.data.extInfo.rtaExtMap={
						param: this.data.param,
						a:this.data.a,
						mobile: this.data.phone,
						orderNo: this.data.orderNo,
	    organization_type: 'hospital',
	    organization_sub_type: 'general_hospital',
	    device_location: 'outpatient_department',
	    device_id: '111111',
	    public_opinion_level: 'low_risk',
	    touch_point_location: 'index',
	    touch_point_type: 'full_screen',
	}

		this.data.interstitialAd.onLoad(() => {
	
			my.request({
			  url: 'https://www.sx2b2c.com/api/Alipayad/getSpaceCodeYd', // 请求的 URL
			  method: 'POST', // POST 方法
			  data: {
				a:this.data.a
			  },
			  header: {
			    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			  },
			  success:(result)=> {
			console.log(result,'99999999999999999')
			this.setData({
				type: result.data.data.type
			})
			my.request({
			  url: 'https://www.sx2b2c.com/api/rf/addLog', // 请求的 URL
			  method: 'POST', // POST 方法
			  data: {
				v1:'911',
				v2:'a='+this.data.a+'orderNo='+this.data.orderNo
			  },
			  header: {
			    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			  },
			})
			// this.data.interstitialAd.show({
			// 	spaceCode:'50_2023111525000070727',
			// 	//全屏
			// 	rtaExtMap: {
			
			// 		organization_type: 'hospital',
			// 		organization_sub_type: 'general_hospital',
			// 		device_location: 'outpatient_department',
			// 		device_id: '111111',
			// 		public_opinion_level: 'low_risk',
			// 		touch_point_location: 'index',
			// 		touch_point_type: 'full_screen',
			// 	}
			// })
			  },
			  fail: function(err) {
			console.log(err,7777777777)
			  }
			})
		
		})

		this.data.interstitialAd.onClose((isClick) => {
			console.log('close', isClick);
		});

		this.data.interstitialAd.onError((err) => {
			console.log('error9', err);
			 this.props.onAdErrors(err)
			//报错统计
			my.request({
			  url: 'https://www.sx2b2c.com/api/Alipayad/iserror', // 请求的 URL
			  method: 'POST', // POST 方法
			  data: {
				orderNo:this.data.orderNo
			  },
			  header: {
			    'Content-Type': 'application/x-www-form-urlencoded' // 设置请求头
			  },
			  success:(result)=> {
						
			  },
			  fail: function() {	
			  }
			})
			//错误跳转地址
		


		});
	}, 
	methods: {
		onBeforeJump(e) {
		  // ... 一系列自定义逻辑
		  // 以下模拟异步函数
		  return new Promise((resolve) => { 
		    
		      resolve(true); // 必须
		   
		  });
		},
		onRenderSuccess() {
		      this.props.onRenderSuccess && this.props.onRenderSuccess()
		    },
		onRenderFail(err){
			this.props.onRenderFail&&this.props.onRenderFail(err)
		}
	},
});